package com.springboot.original.common.utils;

import com.google.common.collect.Sets;

import java.util.Set;
import java.util.concurrent.CountDownLatch;

/**
 * @Auther: ZhangYongMing
 * @Date: 2019/2/20 11:10
 * @Description:
 */
public class ConcurrentTestUtil {

    public static void main(String[] args) throws Exception {

        final int threadC = 30;
        final int loopC = 100;

        final Set<Long> idCounter = Sets.newConcurrentHashSet();
        final IdWorker idWorker = new IdWorker();
        final CountDownLatch latch = new CountDownLatch(threadC * loopC);

        for (int j = 0; j < threadC; j++) {
            new Thread(new Runnable() {
                public void run() {

                    for (int i = 0; i < loopC; i++) {
                        long id = idWorker.nextId();
                        idCounter.add(id);
                        System.out.println(Thread.currentThread().getName()+":"+id);
                        latch.countDown();
                    }

                }
            }).start();
        }

        long st = System.currentTimeMillis();
        latch.await();
        long en = System.currentTimeMillis();
        System.out.println("idCounter:" + idCounter.size() + ",T:" + (en - st) +" ms");
    }

}
